package com.yc.mvc.web;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("param")
public class PathController {

    // MedelAndview 返回视图(JSP,html)
    // => 默认请求转发
    // => 响应重定向
    @RequestMapping({"index","/"})
    public ModelAndView index(){
        ModelAndView mav = new ModelAndView();
        // 设置视图名：redirect: 响应重定向， 否则是请求转发
        mav.setViewName("redirect:../home.html");
        return mav;
    }

    // method 限定请求方法
    @RequestMapping(path = "demo1", method = RequestMethod.GET)
    public String demo1(){
        return "demo1";
    }

    @RequestMapping(path = "demo2", method = RequestMethod.POST)
    public String demo2(){
        return "demo2";
    }
    // 等效于 @RequestMapping(path = "demo1", method = RequestMethod.GET)
    @GetMapping("demo3")
    public String demo3(){
        return "demo3";
    }

    ///////////////////// 请求参数限定 ///////////////////

    @GetMapping(value = "demo4", params = "a")
    public String demo4(){
        return "demo4";
    }

    @GetMapping(value = "demo5", params = "b=1")
    public String demo5(){
        return "demo5";
    }

    @GetMapping(value = "demo6", params = "b!=1")
    public String demo6(){
        return "demo6";
    }

    /////////////////// 头域参数限定 ////////////////////
    @GetMapping(value = "demo7", headers = "host=127.0.0.1:8080")
    public String demo7(){
        return "demo7";
    }

    @PostMapping(value = "demo8", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public String demo8(){
        return "demo8";
    }

    @GetMapping(value = "demo9", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
    public String demo9(){
        return "<h1>demo9</h1>";
    }

    @GetMapping(value = "demo10", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    public String demo10(){
        return "<h1>demo10</h1>";
    }


}
